package com.jfsnpm.plugin.viewoffice;


import java.io.*;
import java.security.*;

public class test {
	public static final char[] hexChar = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	public static final String[] hashTypes = new String[] { "MD2", "MD5",
			"SHA1", "SHA-256", "SHA-384", "SHA-512" };

	public static void main(String[] args) throws Exception {
		args = new String[] {"C:\\zxc\\share\\Enhancing.doc"};
		if (args == null || args.length < 1) {
			System.out
					.println("C:\\zxc\\share\\Enhancing.doc");
			System.exit(1);
		}
		String fileName = args[0];
		System.out.println("需要获取hash的文件为：　" + fileName);
		java.util.List<MessageDigest> mds = new java.util.ArrayList<MessageDigest>();
		for (String hashType : hashTypes) {
			MessageDigest md = MessageDigest.getInstance(hashType);
			mds.add(md);
		}
		InputStream fis = null;
		try {
			fis = new FileInputStream(fileName);
			byte[] buffer = new byte[1024];
			int numRead = 0;
			while ((numRead = fis.read(buffer)) > 0) {
				for (MessageDigest md : mds) {
					md.update(buffer, 0, numRead);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
		for (MessageDigest md : mds) {
			System.out.println(md.getAlgorithm() + " == "
					+ toHexString(md.digest()));
			System.out.println(md.getAlgorithm() + " == "
					+ encode(md.digest()));
		}
	}

	public static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
			sb.append(hexChar[b[i] & 0x0f]);
		}
		return sb.toString();
	}
	
	static public String encode(byte[] data) {  
        char[] out = new char[((data.length + 2) / 3) * 4];  
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {  
            boolean quad = false;  
            boolean trip = false;  
            int val = (0xFF & (int) data[i]);  
            val <<= 8;  
            if ((i + 1) < data.length) {  
                val |= (0xFF & (int) data[i + 1]);  
                trip = true;  
            }  
            val <<= 8;  
            if ((i + 2) < data.length) {  
                val |= (0xFF & (int) data[i + 2]);  
                quad = true;  
            }  
            out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];  
            val >>= 6;  
            out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];  
            val >>= 6;  
            out[index + 1] = alphabet[val & 0x3F];  
            val >>= 6;  
            out[index + 0] = alphabet[val & 0x3F];  
        }  
        return  String.valueOf(out);  
    }
	static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="  
            .toCharArray();
}
